var role = {
		init : function() {
			role.roleGrid = $('#roleGrid');
			role.roleMenu = $('#roleMenu');
			role.roleAddDialog = $('#roleAddDialog');
			role.roleAddForm = $('#roleAddForm');
			
			role.roleUpdateForm = $('#roleUpdateForm');
			role.roleMenuUpdate = $('#roleMenuUpdate')
			role.roleUpdateDialog = $('#roleUpdateDialog');
			role.name=$('#roleName');
			
			//console.info(role.roleGrid);
			role.roleGrid.datagrid({
			    url: window.ctx +'/RoleServlet?command=listRole',
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 5,
			    pageList: [5, 10, 15, 20, 25],
			    pageNumber: 1,
			   /*  sortName: 'age', */
			    sortOrder: 'desc',
			    toolbar: '#roleToolbar',
			    columns:[[
					{field:'id', width:100, checkbox: true},
					{field:'name', title:'角色', width:100, align:'center'},
					{field:'operateTime', title:'操作时间', width:100, align:'center'},
					{field:'stateStr', title:'状态', width:100, align:'center'},
					//{field:'operate', title: '操作', formatter: role.row.showRole, align:'center'},
					// {field:'memo', title:'备注', width:100, align:'center'}
			    ]],
				onDblClickRow: function(rowIndex, rowData) {
					role.toolbar.showUpdateDialog(rowData.id);
				},
				onBeforeLoad: function (param) {
					var role=$('#role').val();
					param.role=role;
			    },
			});
		},
		toolbar: {
			showAddDialog: function() {
				role.roleMenu.combotree({
					url: window.ctx +'/MainServlet?command=listMenu',
					lines: true,
					multiple: true,
					checkbox: true,
					onlyLeafCheck: true,
					onLoadSuccess: function(node, data) {
						$(this).tree('expandAll')
					}
				}); 
				// role.toolbar.showCombotree();
				role.roleAddDialog.dialog('open');
				role.name.focus();
				// 坑，只能使用id选择器
				/* role.age.numberspinner('setValue', 20); */
			},
			hiddenAddDialog: function() {
				role.roleAddDialog.dialog('close');
				role.toolbar.resetAddForm();
			},
			resetAddForm: function() {
				role.roleAddForm.form('reset');
			},
			add: function() {
				if (role.roleAddForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					var params = role.toolbar.getParams(role.roleMenu, role.roleAddForm.serialize());
					$.post(window.ctx +'/RoleServlet?command=addRole', params, function(data) {
						if (data = 1) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							$.messager.progress('close');
							role.toolbar.resetAddForm();
							role.roleGrid.datagrid('reload');
							
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error')
						}
						$.messager.progress('close');
						role.toolbar.hiddenAddDialog();
					});
				}
			},
			getParams: function(roleMenu, defaultSerialize) {
				var treeObj = roleMenu.combotree('tree');
				var indeterminateNodes = treeObj.tree('getChecked', 'indeterminate');
				var indeterminateNodeIds = [];
				$.each(indeterminateNodes, function(i, v) {
					indeterminateNodeIds.push('&roleMenu=' + v.id);
				});
				return defaultSerialize + indeterminateNodeIds.join('');
			},
			showUpdateDialog: function(id) {
				var getData = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx +'/RoleServlet?command=getRole', {id : v}, function(data) {
						if (data) {
							role.roleUpdateForm.form('load', {
								id: data.id,
								roleName: data.name,
								roleMemo: data.memo
							});
							
							// 测试默认选中id=1,2,3,4
							var checkedItems = [];
							if (data.menuIds) {
								checkedItems = data.menuIds.split(',');
							}
							
							role.roleMenuUpdate.combotree({
								url: window.ctx +'/MainServlet?command=listMenu',
								lines: true,
								multiple: true,
								checkbox: true,
								onlyLeafCheck: false,
								onLoadSuccess: function(node, data) {
									var that = this;
									$(that).tree('expandAll');
									if (data) {
										$(data).each(function(index, value) {
											if ($.inArray(value.id + '', checkedItems) != -1) {
												$(that).tree('check', value.target);
											} else {
												$(that).tree('uncheck', value.target);
											}
										});
									}
								}
							});
							role.roleUpdateDialog.dialog('open');
						} else {
							$.messager.alert('系统提示', '获取信息失败！', 'error')
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getData(id);
				} else {
					var row = role.roleGrid.datagrid('getSelections');
					if (row.length == 1) {
						getData(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			hiddenUpdateDialog: function() {
				role.roleUpdateDialog.dialog('close');
				role.toolbar.resetUpdateForm();
			},
			resetUpdateForm: function() {
				role.roleUpdateForm.form('reset');
			},
			update: function() {
				if (role.roleUpdateForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					var params = role.toolbar.getParams(role.roleMenuUpdate, role.roleUpdateForm.serialize());
					$.post(window.ctx +'/RoleServlet?command=updateRole', params, function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							role.toolbar.resetUpdateForm();
							role.roleGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
						role.toolbar.hiddenUpdateDialog();
					});
				}
			},
			remove: function() {
				var row = role.roleGrid.datagrid('getSelections');
				if (row.length > 0) {
					var ids = [];
					$.each(row, function(i, v) {
						ids.push(v.id);
					});
					$.post(window.ctx +'/RoleServlet?command=deleteRole', {ids: ids.join(',')}, function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '删除成功！',
								timeout: 3000,
								showType: 'slide'
							});
							role.roleGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '删除失败！', 'error')
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择数据进行删除！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
			},
			redo: function() {
				role.roleGrid.datagrid('unselectAll');
			},
			updateState: function() {
				var row = role.roleGrid.datagrid('getSelections');
				if (row.length == 1) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx +'/RoleServlet?command=changeRole', {id : row[0].id} , function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							role.roleGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',	
						msg: '请选择一行数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
		}
		},
		
	}

	
	$(function(){
		role.init();
	});